(function(){
	$.fn.extend({
//		options = {
//			'data':[],
//			'contentDefine':{
			//	'link':'',
			//	'author':'',
			//	'type':'',
			//	'date':'',
			//	'body':'',
			//	"data-tab-toggle":"",
			//	"data-tab-id":'',
			//	"data-tab-url":'',
			//	"data-tab-title":''
//			}
//		}
		knowledgeList:function(options){
			var $this = this;
			var contentDefine = options.contentDefine || {};
			
			var outputErrorContent = function(errorText) {
				$this.parent().addClass("t-error");
				$this.find(".error-prompt").remove();
				var $errorContent = $("<p></p>");
				$errorContent.addClass("error-prompt");
				$errorContent.text(errorText);
				$this.append($errorContent);
			};
			
			if(!options.data || options.data.length <= 0) {
				outputErrorContent("╮(╯﹏╰)╭  没有为你搜索到内容");
				return ;
			}
			$this.parent().removeClass("t-error");
			// 获取 data-tab-xxx的值，它有可能是一个函数，也有可能是一个内容定义
			var getTabData = function(index, item, key) {
				var mKey = 'data-tab-' + key;
				if ( typeof contentDefine[mKey] == "function" ) {
					var val = contentDefine[mKey](index, item);
					return val;
				} else {
					if (contentDefine[mKey]) {
						return item[contentDefine[mKey]];
					}
					return '';
				}
			};
			var buildItem = function(index, item) {
				var $item = $("<div class='t-item'/>");
				$item.data("order", (index + 1 ));
				var $header = $("<header/>");
				var $a = $("<a class='t-link'/>");
				$a.text(contentDefine.link ? item[contentDefine.link] : '');
				$a.attr({
					"href" : "javascript:void(0)",
					"data-tab-toggle" : getTabData(index, item, 'toggle'),
					"data-tab-id" : getTabData(index, item, 'id'),
					"data-tab-url" : getTabData(index, item, 'url'),
					"data-tab-title" : getTabData(index, item, 'title'),
					"data-tab-single-body" : true
				});
				$header.append($a);
				var $nav = $("<div/>");
				var outputLabel = function($container, label, text, className) {
					var $label = $("<label class='t-label'/>");
					$label.addClass(className);
					$label.text(label + "：" + text);
					$container.append($label);
				};
				outputLabel($nav, "作者", ((contentDefine.author && item[contentDefine.author]) ? item[contentDefine.author] : ''), 't-author');
				outputLabel($nav, "类型", ((contentDefine.type && item[contentDefine.type]) ? item[contentDefine.type] : ''), 't-type');
				outputLabel($nav, "时间", ((contentDefine.date && item[contentDefine.date]) ? item[contentDefine.date] : ''), 't-date');
				
				var $article = $("<article/>");
				var $p = $("<p class='t-body'/>");
				var getBodyContent = function(){
					return (contentDefine.body && item[contentDefine.body]) ? ("内容：" + item[contentDefine.body]) : '内容：';
				};
				var body = getBodyContent();
				$p.append(body);
				$article.append($p);
				
				$item.append($header);
				$item.append($nav);
				$item.append($article);
				return $item;
			};
			$this.empty();
			$.each(options.data,function(index, item){
				var $item = buildItem(index, item);
				$this.append($item);
			});
		}
	});
})(jQuery);

var KnowledgeSearchPage = {
	// 查询搜索结果成功
	onFindSearchResultSucc:function(data, status, xhr) {
		Logger.warn('search -> succ');
		$("#searchResultFrm").find("#knowledgeSearchResultPagination").show();
		if(knowledgeSearchResultPagination){
			knowledgeSearchResultPagination.count = data.info.count;
			knowledgeSearchResultPagination.refresh(false); // 不重新请求数据
		}
		$(".t-knowledge-list").knowledgeList({
			'data':data.info.data,
			'contentDefine':{
				'link':'title',
				'author':'create_user',
				'type':'category_name',
				'date':'create_time',
				'body':'content_str',
				"data-tab-toggle":function(index, item){
					return ".m-tab-container";
				},
				"data-tab-id":'question_id',
				"data-tab-url":function(index, item) {
					return ctx + '/sms/knowledge/repos/showx/'+item['question_id'];
				},
				"data-tab-title":'title'
			}
		});
		Logger.warn("length = " + ($("*[data-tab-toggle='.m-tab-container']").length));
		$("*[data-tab-toggle='.m-tab-container']").tabset({
			"$parentBody":$(".t-knowledge-search"),
			"preClose":function(){ 
				// 关闭页面之前执行的方法
			},
			"preOpen":function(){ 
				// 打开页面之前执行的方法
			},
			"preActiveTab":function(){ // 激活某个标签页之前执行的方法
			},
			'custDeal': function(id, name, url) {
				parent.KnowledgeReposPage.activeMenu(id, name, url);
			}
		});
	}
};